//
//  RadarHorizontalCell.m
//  carcareIOS
//
//  Created by 王然 on 16/6/30.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "RadarHorizontalCell.h"
#import "LocOneWayCell.h"
#import "OneWayMgr.h"
#import "MapTrajectory.h"

typedef NS_ENUM(NSInteger , View_Type){
    View_Type_MapBackV = 100,//轨迹地图背景view
    View_Type_MapNoDataV ,//轨迹地图没有数据的view
} ;

#define SECTION_HEIGT (10)
#define CELL_MAP_HEIGHT (134.5 * __MAIN_RATIO_375 + SECTION_HEIGT)

@interface RadarHorizontalCell()<UITableViewDelegate , UITableViewDataSource>

@property (nonatomic , strong) UITableView * tableView;

@property (nonatomic , readonly) NSArray * titleArray;

@property (nonatomic , strong) OneDriveModel * oneDriveModel;

@property (nonatomic , strong) OneWayMgr *oneWayMgr;

@property (nonatomic, strong) MapTrajectory * mapTrajectory;

@property (nonatomic, assign) CGFloat lastOffsetY;

@property (nonatomic , strong) UIView * mapNoDataV;

@end

@implementation RadarHorizontalCell

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, __MAIN_HEIGHT_64 - TOP_HEIGHT) style:UITableViewStylePlain];
        self.tableView.backgroundColor = [Theme backGroundColor];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:self.tableView];
        [self mapTrajectory];
    }
    return self;
}
#pragma mark - tableView delegate
-(NSArray *)titleArray
{
    return @[@"雷达图",@"空白",@"轨迹地图",@"起始地址",@"空白"];
}
#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.titleArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (indexPath.row == 0 ? [RadarOneWayCell getCellHeight] : (indexPath.row == 2 ? (self.oneDriveModel.gpsTrackBool ? CELL_MAP_HEIGHT : 0) : (indexPath.row == 3 ? [LocOneWayCell getCellHeight] : (indexPath.row == 1 ? (self.oneDriveModel.gpsTrackBool ?(SECTION_HEIGT * 3) : SECTION_HEIGT): SECTION_HEIGT ))));
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell;
    switch (indexPath.row) {
        case 0:
        {
            NSString *identifier = @"RadarOneWayCell";
            cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            
            if (!cell) {
                cell = [[RadarOneWayCell alloc] initWithStyle:UITableViewCellStyleValue1
                                              reuseIdentifier:identifier];
            }
            
            ((RadarOneWayCell *)cell).driveModel = self.oneDriveModel;
        }
            break;
        case 2:
        {
            NSString *identifier = @"MapCell";
            cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                              reuseIdentifier:identifier];
                
                UIView * mapBackV = [[UIView viewWithFrame:CGRectMake(SECTION_HEIGT,SECTION_HEIGT, __MAIN_WIDTH - SECTION_HEIGT * 2, CELL_MAP_HEIGHT - SECTION_HEIGT)] setupOnView:cell];
                mapBackV.clipsToBounds = YES;
                mapBackV.tag = View_Type_MapBackV;
                
                UIView * mapNoDataV = [self noDataViewWithFrame:CGRectMake(0, 0, mapBackV.width, mapBackV.height - SECTION_HEIGT)];
                mapNoDataV.tag = View_Type_MapNoDataV;
                mapNoDataV.hidden = YES;
                [mapBackV addSubview:mapNoDataV];
                
            }
            UIView * mapBackV = [cell viewWithTag:View_Type_MapBackV];
            self.mapNoDataV = [cell viewWithTag:View_Type_MapNoDataV];

            if (self.oneDriveModel.gpsTrackBool) {
                if (self.oneDriveModel.gpss.count > 0) {
                    [mapBackV addSubview:self.mapTrajectory.mapView];
                    
                    __weak __typeof(self) wself = self;
                    [NSTimer scheduledTimerWithTimeInterval:0.01 block:^{
                        [wself.mapTrajectory reloadPolyLine];
                    } repeats:NO];
                
                }
                //无轨迹地图数据，但用户打开了显示轨迹按钮的情况 设置显示无数据页面否则隐藏
                [self.mapNoDataV setHidden:self.oneDriveModel.gpss.count > 0];
            }
        }
            break;
        case 3:
        {
            NSString *identifier = @"LocOneWayCell";
            cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            
            if (!cell) {
                cell = [[LocOneWayCell alloc] initWithStyle:UITableViewCellStyleValue1
                                            reuseIdentifier:identifier];
                
            }
            [((LocOneWayCell *)cell)reloadStarLoc:self.oneDriveModel.startPlace withEndLoc:self.oneDriveModel.endPlace];
        }
            break;
            
            //空白cell
        case 1:
        case 4:
        {
            NSString * cellID = (indexPath.row == 1 ? @"haveTitleCellID" : @"noCellID");
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            }
            cell.backgroundColor = [Theme backGroundColor];
            if(self.oneDriveModel.gpsTrackBool)
            {
                cell.textLabel.text = (indexPath.row == 1 ? @"行驶轨迹" : @"");
                cell.textLabel.font = FONT_DEFAULT_Light(14);
                cell.textLabel.textColor = [Theme wordGrayColor];
            }
            
        }
            break;
        default:
            break;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //点击地图 跳转到轨迹页面
    if (indexPath.row == 2) {
        [[StatisticsMgr sharedInstance] addClickID:@"单次行程页面轨迹地图点击" description:[self.tableView cellForRowAtIndexPath:indexPath].description];
        [UmengMgr event:Tirp_GPS];
        if (self.oneDriveModel.gpsTrackBool && self.oneDriveModel.gpss.count > 0) {
            if (self.mapClick) self.mapClick();
        }
    }
}
- (void)setSelectIndex:(NSInteger)selectIndex
{
    _selectIndex = selectIndex;
    self.oneDriveModel = DRIVERSMGR.selelctDrivesModel.travelModels[selectIndex];
    if(self.oneDriveModel.gpsTrackBool)[self reloadGpsInfosData:selectIndex];
    [self.tableView setContentOffset:CGPointMake(0, 0)];
    [self.tableView reloadData];
    
}
#pragma mark - 请求轨迹使用的数据
- (void)reloadGpsInfosData:(NSInteger)selectIndex
{
    if (self.oneDriveModel.gpss.count == 0) {
        __weak __typeof(self) wself = self;
        [self.oneWayMgr getGpsWithStartTime:self.oneDriveModel.startTimeGps withEndTime:self.oneDriveModel.endTimeGps Success:^(NSArray *gpss, NSArray *types) {
            wself.oneDriveModel.gpss = gpss;
            wself.oneDriveModel.gpsTypes = types;
            wself.oneDriveModel.speedUpTimes = wself.oneWayMgr.speedUpTimes;
            wself.oneDriveModel.speedDownTimes = wself.oneWayMgr.speedDownTimes;
            
            //无轨迹地图数据，但用户打开了显示轨迹按钮的情况 设置显示无数据页面否则隐藏
            [wself.mapNoDataV setHidden:self.oneDriveModel.gpss.count > 0];
            [wself.tableView reloadData];
        } fail:^{
            [wself.mapNoDataV setHidden:YES];
        }];
        
    }
}
#pragma mark - Scroll delegate
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if ((scrollView.contentOffset.y > __MAIN_HEIGHT_64 / 2) && self.lastOffsetY < scrollView.contentOffset.y) {
        [UmengMgr event:Trip_Scroll];
        [[StatisticsMgr sharedInstance] addClickID:@"单次行程页面向下滑动" description:scrollView.description];
    }
    self.lastOffsetY = scrollView.contentOffset.y;
}
#pragma mark - 无数据的轨迹显示view
- (UIView *)noDataViewWithFrame:(CGRect)frame
{
    UIView * mapNoDataV = [[[UIView viewWithFrame:frame] resetBackgroundColor:COLOR_HEXSTRING(@"#f8f8f8")] resetCornerRadius:2];
    UIImage * sadImage = ImageNamed(@"sadFaceTraNoDataImage");
    UIImageView * sadFaceImageV = [[UIImageView imageViewWithFrame:CGRectMake(0, 0, sadImage.size.width, sadImage.size.height) image:sadImage] setupOnView:mapNoDataV];
    UILabel * noDataHeadL = [[UILabel labelWithFrame:CGRectZero font:FONT_DEFAULT_Bold(16) text:@"糟糕!" textColor:COLOR_HEXSTRING(@"#bdbdbd")] setupOnView:mapNoDataV];
    CGFloat labelH = 21;
    CGFloat labelCenterY = 3;
    [[Factory flabelWithWidth:(__MAIN_WIDTH - sadImage.size.width - SECTION_HEIGT * 2 ) font:FONT_DEFAULT_Bold(16) text:@"轨迹数据丢失了!" textColor:COLOR_HEXSTRING(@"#bdbdbd") onView:mapNoDataV resetSizeFinish:^(UILabel *label) {
        [label setSize:CGSizeMake(label.size.width, labelH)];
        [noDataHeadL setSize:CGSizeMake(label.size.width, labelH)];
        
        CGFloat imageAndLabelX = 15;
        CGFloat viewW = sadImage.size.width + imageAndLabelX + label.size.width;
        [sadFaceImageV setCenter:CGPointMake((mapNoDataV.width - viewW) / 2 + sadImage.size.width / 2, mapNoDataV.height / 2)];
        [label setCenter:CGPointMake((mapNoDataV.width - viewW) / 2 + sadImage.size.width + imageAndLabelX + label.size.width / 2, mapNoDataV.height / 2 + labelCenterY + labelH / 2)];
        [noDataHeadL setCenter:CGPointMake(label.centerX, mapNoDataV.height / 2 - labelCenterY - labelH / 2)];
        
    }] setupOnView:mapNoDataV];
    return mapNoDataV;
}
-(OneWayMgr *)oneWayMgr{
    if (!_oneWayMgr) {
        _oneWayMgr = [[OneWayMgr alloc] init];
    }
    return _oneWayMgr;
}

-(MapTrajectory *)mapTrajectory{
    if (self.oneDriveModel.gpsTrackBool && self.oneDriveModel.gpss.count > 0 && !_mapTrajectory) {
        _mapTrajectory = [[MapTrajectory alloc]initWithType:Map_Type_NoTouch withModel:self.oneDriveModel];
        _mapTrajectory.mapView.userInteractionEnabled = NO;
        _mapTrajectory.mapView.frame = CGRectMake(0, 0, __MAIN_WIDTH - SECTION_HEIGT * 2, CELL_MAP_HEIGHT - SECTION_HEIGT);
    }
    return _mapTrajectory;
}

@end
